package com.test.springCloud.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.test.springCloud.mapper.ArticleMapper;
import com.test.springCloud.mapper.UserMapper;
import com.test.springCloud.service.ArticleService;
import com.test.springCloud.utils.JwtUtil;
import com.test.springcloud.pojo.ArticlePojo;
import com.test.springcloud.pojo.RolePojo;
import com.test.springcloud.pojo.UserPojo;
import org.springframework.stereotype.Service;

import javax.accessibility.AccessibleContext;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("ArticleService")
public class ArticleServiceImpl implements ArticleService {

    @Resource
    ArticleMapper articleMapper;

    @Resource
    UserMapper userMapper;

    /**
     * 添加文章数据
     * @param req
     * @param request
     * @return
     */
    public String addArticle(JSONObject req, HttpServletRequest request) {

        JSONObject js = new JSONObject();
        //解析token
        String token = request.getHeader("token");
        String id = JwtUtil.getUserId(token);
        Integer userId = Integer.parseInt(id);
        //跟据id查用户信息
        UserPojo user = userMapper.getUserById(userId);
        RolePojo role = userMapper.getRoleById(user.getRoleId());
        //只有老师能添加数据
        if ("老师".equals(role.getRoleName())){
            ArticlePojo art = new ArticlePojo();
            String articleName = req.getString("articleName");
            if ("".equals(articleName)){
                js.put("code",4020);
                js.put("msg","文章名不能为空！");
                return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
            }else {
                art.setArticleName(articleName);
            }
            art.setArticleCreateName(user.getUserName());
            //创建时间设定为当前时间
            Date date = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sf.format(date);
            art.setArticleCreateTime(time);
            art.setArticleUpdateTime(time);
            art.setArticleUpdateName(user.getUserName());
            //文本内容
            String articleContent = req.getString("articleContent");
            if ("".equals(articleContent)){
                js.put("code",4020);
                js.put("msg","无法发送空白文章！");
                return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
            }else {
                art.setArticleContent(articleContent);
            }
            //添加文章
            int size = articleMapper.addArticle(art);
            if (size>0){
                js.put("code",200);
                js.put("msg","文章发表成功！");

                return JSONObject.toJSONString(js,SerializerFeature.WriteMapNullValue);
            }
        }
        js.put("code",4021);
        js.put("msg","文章发表失败！请检查数据后重新操作！");

        return JSONObject.toJSONString(js,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 获取全部数据信息
     * @return
     */
    public String getAllArticle(Integer page,Integer limit) {

        JSONObject js = new JSONObject();
//        Integer page = req.getInteger("page");
//        Integer limit = req.getInteger("limit");
        Integer start = (page-1)*limit;
        List<ArticlePojo> articleList = articleMapper.getAllArticle(start,limit);
        List<ArticlePojo> articlesize = articleMapper.getAllArticles();
        int size = articlesize.size();
         
        js.put("code",200);
        js.put("msg","数据获取成功！");
        js.put("data",articleList);
        js.put("page",size);

        return JSONObject.toJSONString(js,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 跟据id获取文章内容
//     * @param req
     * @return
     */
    public String getArticleByid(Integer page,Integer limit,Integer newsClassId) {

        JSONObject js = new JSONObject();
//        Integer articleId = req.getInteger("articleId");
        Integer articleId = newsClassId;
        Integer start = (page-1)*limit;
        ArticlePojo article = articleMapper.getArticleById(start,limit,articleId);
        ArticlePojo articlePojo = articleMapper.getArticleByIds(articleId);

        js.put("code",200);
        js.put("msg","数据获取成功！");
        js.put("data",article);
        js.put("page",1);

        return JSONObject.toJSONString(js,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 跟据文章id更新文章数据（文章名和文章内容）
     * @param req
     * @param request
     * @return
     */
    public String updateArticleByid(JSONObject req, HttpServletRequest request) {
        JSONObject js = new JSONObject();
        Integer articleId = req.getInteger("articleId");
        String articleName = req.getString("articleName");
        String articleContent = req.getString("articleContent");
        //解析token
        String token = request.getHeader("token");
        String id = JwtUtil.getUserId(token);
        Integer userId = Integer.parseInt(id);
        //跟据id查用户信息
        UserPojo user = userMapper.getUserById(userId);
        RolePojo role = userMapper.getRoleById(user.getRoleId());
        //只有老师能修改数据
        if ("老师".equals(role.getRoleName())){
            List<ArticlePojo> list = new ArrayList<ArticlePojo>();
            ArticlePojo article = new ArticlePojo();
            article.setArticleName(articleName);
            article.setArticleContent(articleContent);
            list.add(article);
            int size = articleMapper.updateArticleById(list,articleId);
            if (size>0){
                js.put("code",200);
                js.put("msg","更新成功！");

                return JSONObject.toJSONString(js,SerializerFeature.WriteMapNullValue);
            }
            js.put("code",4023);
            js.put("msg","更新失败，请重试！");
        }
        return JSONObject.toJSONString(js,SerializerFeature.WriteMapNullValue);
    }

    /**
     * 根据id删除文章
     * @param req
     * @param request
     * @return
     */
    public String deleteArticleById(JSONObject req, HttpServletRequest request) {
        JSONObject js = new JSONObject();
        Integer articleId = req.getInteger("articleId");
        //解析token
        String token = request.getHeader("token");
        String id = JwtUtil.getUserId(token);
        Integer userId = Integer.parseInt(id);
        //跟据id查用户信息
        UserPojo user = userMapper.getUserById(userId);
        RolePojo role = userMapper.getRoleById(user.getRoleId());
        //只有老师能修改数据
        return null;
    }
}
